﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Inference
{
    public partial class Consult : Form
    {
        public bool success;
        public int IdAnswer;
        int DefVal;
        List<int> IllegalValues;

        public Consult()
        {
            InitializeComponent();
        }

        List<RadioButton> rb = new List<RadioButton>();

        public void SetQuestion(string quest,Dictionary<int,string> variants,int DefValue,List<int> IllegalVals)
        {
            label1.Text = quest;
            DefVal = DefValue;
            IllegalValues = IllegalVals;
            Dictionary<int, string> vars = new Dictionary<int, string>();
            foreach (KeyValuePair<int, string> rp in variants)
                vars.Add(rp.Key,rp.Value);
            int loc=10;
            if (label1.Height > 18)
            {
                RadioPanel.Top = label1.Bottom + 20;
                this.Height = RadioPanel.Bottom + 90;
            }
            //if (!(DefValue==0 && IllegalValues.Count()==0))
            vars.Add(-1,"Не знаю");
            
            foreach (KeyValuePair<int, string> rp in vars)
            {
                AddRadio(loc,rp.Value,rp.Key);
                loc += 25;
            }
            if (loc > RadioPanel.Height)
            {
                RadioPanel.Height = loc + 20;
                this.Height = RadioPanel.Bottom + 90;
            };
            rb[0].Checked = true;
        }

        private void AddRadio(int location,string text,int id)
        {
            RadioButton rad = new RadioButton();
            rb.Add(rad);
            //if (id == DefVal)
            //    rad.ForeColor = Color.Green;
            //if (IllegalValues.Contains(id))
            //    rad.ForeColor = Color.Red;
            rad.AutoSize = true;
            rad.Location = new System.Drawing.Point(10, location);
            rad.Name = "rad" + text;
            rad.Size = new System.Drawing.Size(85, 17);
            rad.TabIndex = 0;
            rad.TabStop = true;
            rad.Text = text;
            rad.Tag = id;
            rad.CheckedChanged += new EventHandler(rad_CheckedChanged);
            rad.UseVisualStyleBackColor = true;
            RadioPanel.Controls.Add(rad);
            rad.Checked = true;
        }

        void rad_CheckedChanged(object sender, EventArgs e)
        {
            if (((RadioButton)sender).Checked)
                IdAnswer = Convert.ToInt32(((RadioButton) sender).Tag);
        }

        private void BtnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Consult_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("Вы уверены, что хотите выйти?", "", MessageBoxButtons.YesNo,
                MessageBoxIcon.None,MessageBoxDefaultButton.Button1) == DialogResult.Yes)
            {
                success = false;
            }
            else
                e.Cancel = true;
        }

        private void BtnNext_Click(object sender, EventArgs e)
        {
            success = true;
            this.FormClosing -= Consult_FormClosing;
            this.Close();
        }

        private void Consult_Load(object sender, EventArgs e)
        {

        }

    }
}
